package com.example.woke;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.zhongcai.online.R;
import com.woke.fragment.ExchangeFragment;

public class ZhongjinsuoActivity extends FragmentActivity implements OnCheckedChangeListener, OnPageChangeListener {
	
	private static final String TAG=ZhongjinsuoActivity.class.getSimpleName();
	
	private RadioButton mLoanRb;
	private RadioButton mCreditRb;
	private RadioGroup mRadioGroup;
	private ViewPager mViewPager;
	private ChangePromoteAdapter mPromoteAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhongjinsuo);
		
		findViewById(R.id.avmywalletnocard_image_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		mRadioGroup = (RadioGroup) findViewById(R.id.changeRg);
		mLoanRb = (RadioButton) findViewById(R.id.loanRb);
		mCreditRb = (RadioButton) findViewById(R.id.creditRb);
		mRadioGroup.setOnCheckedChangeListener(this);

		mLoanRb.setChecked(true);
		
		
		mPromoteAdapter = new ChangePromoteAdapter(getSupportFragmentManager());
		mViewPager.setAdapter(mPromoteAdapter);
		mViewPager.setOnPageChangeListener(this);
		mViewPager.setCurrentItem(0);
			
	}
	

	
//	private OnClickListener listener = new OnClickListener() {
//		
//		@Override
//		public void onClick(View v) {
//			// TODO Auto-generated method stub
//			Intent intent = new Intent(ZhongjinsuoActivity.this,PeaceActivity.class);
//			intent.setFlags(Integer.parseInt(v.getTag().toString()));
//			startActivity(intent);
//		}
//	};
	

	
	
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.creditRb:
			mViewPager.setCurrentItem(0, false);
			break;
		case R.id.loanRb:
			mViewPager.setCurrentItem(1, false);
			break;
		default:
			break;
		}

	}

	/*----------------------------------------------------------------------*/

	private class ChangePromoteAdapter extends FragmentPagerAdapter {

		public ChangePromoteAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}


		@Override
		public Fragment getItem(int arg0) {
			ExchangeFragment fragment=new ExchangeFragment();
			Bundle args=new Bundle();
			if(arg0==0){
				args.putInt("type", ExchangeFragment.TYPE_CREDIT);
			}else{
				args.putInt("type", ExchangeFragment.TYPE_LOAN);
			}
			fragment.setArguments(args);
			return fragment;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 2;
		}

	}
	

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
	if(arg0==0){
		mCreditRb.setChecked(true);
		mLoanRb.setChecked(false);
	}else{
		mCreditRb.setChecked(false);
		mLoanRb.setChecked(true);
	}
		
	}
}
